package www.qiyue.email;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.text.StrSubstitutor;

import java.util.HashMap;

/**
 * 发送邮件工具类
 */
public final class MailUtils {


    /**
     * 发送邮件
     * 参数一:发送邮件给谁
     * 参数二:发送邮件的内容
     */
    public static void sendMail(String toEmail, String emailMsg,String subject) throws Exception {
        MailAccount account = new MailAccount();
        account.setHost("smtp.163.com");
        account.setPort(25);
        account.setAuth(true);
        account.setFrom("dh13776827295@163.com");
        account.setUser("dh13776827295@163.com");
        account.setPass("JBJMYKGKJAJWSRFV"); //密码
        MailUtil.send(account, CollUtil.newArrayList(toEmail), subject, emailMsg, true);

        /*//创建HemlEmail对象，封装邮件发送参数
        HtmlEmail htmlEmail = new HtmlEmail();
        //你的邮件服务器的地址
        htmlEmail.setHostName("smtp.163.com");
        //注意：参数1--邮箱账号   参数2--邮件发送授权码
        htmlEmail.setAuthentication("dh13776827295@163.com", "JBJMYKGKJAJWSRFV");
        //设置发件人， 其中参数代表发件人邮件地址
        htmlEmail.setFrom("dh13776827295@163.com");
        //设置收件人，如果想添加多个收件人，将此语句多写几次即可。其中参数代表收件人邮件地址
        htmlEmail.addTo(toEmail);
        //设置邮件主题
        htmlEmail.setSubject(subject);
        //设置编码格式及正文内容
        htmlEmail.setCharset("gb2312");
        htmlEmail.setMsg(emailMsg);
        htmlEmail.send();//发送邮件
        MailUtil.send(htmlEmail, CollUtil.newArrayList("alichen3116@aliyun.com"), "测试", "邮件来自Hutool测试", false);*/

    }

    public static void main(String[] args) throws Exception {
        String toEmail = "402382440@qq.com";
        String emailMsg = "<h1>${customerId}账单</h1><br/> ${start_date}至${end_date},贵公司需缴纳租金${fee}元(大写${fee_DX}整),请于${fee_date}前支付.";
        HashMap<String, String> map = new HashMap<>();
        map.put("customerId","test");
        StrSubstitutor strSubstitutor = new StrSubstitutor(map);
        String replace = strSubstitutor.replace(emailMsg);
        String subject = "账单";
        sendMail(toEmail,replace,subject);
        System.out.println("发送成功。。。");




    }
}









